﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GatewayInspect.Common;
using GatewayInspect.Model;

namespace GatewayInspect.Modules
{
    public partial class YHGLModule : BaseModule
    {
        public YHGLModule()
        {
            InitializeComponent();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            this.toolStripSeparator1.Visible = false;
            this.tsBtnImportExcel.Visible = false;
            this.tsBtnDownTemplate.Visible = false;
            this.tsBtnSave.Visible = false;
        }

        protected override void OnLoadModule()
        {
            base.OnLoadModule();

            string strSql = "select ID,USERNAME,PASSWORD,ROLE,DECODE(ROLE,'0','普通用户','1','管理员','') as ROLE_XS,XM,ISSYSTEM,DECODE(ISSYSTEM,'1','是','否') as ISSYSTEM_XS " +
                "from T_SYS_USER where ORGCODE = '" + Session.CurrentOrgCode + "' ORDER BY ISSYSTEM DESC, ROLE DESC, USERNAME";
            DataTable dtYHGL = DataAccess.ExecuteDataTable(strSql);

            this.cdgvYHGL.AutoGenerateColumns = false;
            this.cdgvYHGL.DataSource = dtYHGL;
        }

        protected override void OnNew()
        {
            base.OnNew();

            using (YHGLForm form = new YHGLForm())
            {
                if (form.ShowDialog() == DialogResult.OK)
                {
                    MessageBox.Show("新增用户成功！");
                    OnLoadModule();
                }
            }
        }

        protected override void OnModify()
        {
            base.OnModify();

            if (cdgvYHGL.SelectedCells != null && cdgvYHGL.SelectedCells.Count > 0)
            {
                DataGridViewRow row = cdgvYHGL.SelectedCells[0].OwningRow;
                UserInfo _user = new UserInfo();
                _user.ID = row.Cells["ID"].Value.ToString();
                _user.UserName = row.Cells["USERNAME"].Value.ToString();
                _user.Password = row.Cells["PASSWORD"].Value.ToString();
                _user.XM = row.Cells["XM"].Value.ToString();
                _user.Role = row.Cells["ROLE"].Value.ToString();
                _user.IsSystem = row.Cells["ISSYSTEM"].Value.ToString();
                using (YHGLForm form = new YHGLForm(_user))
                {
                    if (form.ShowDialog() == DialogResult.OK)
                    {
                        MessageBox.Show("修改用户信息成功！");
                        OnLoadModule();
                    }
                }
            }
        }

        protected override void OnDelete()
        {
            base.OnDelete();
            if (cdgvYHGL.SelectedCells != null && cdgvYHGL.SelectedCells.Count > 0)
            {
                if (MessageBox.Show("确定要删除选中用户吗?", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DataGridViewRow row = cdgvYHGL.SelectedCells[0].OwningRow;
                    if (row.Cells["ISSYSTEM"].Value.ToString() == "1")
                    {
                        MessageBox.Show("系统内置用户不能删除！");
                        return;
                    }
                    string strID = row.Cells["ID"].Value.ToString();
                    int i = DataAccess.ExecuteNonQuery("delete from T_SYS_USER where ID = '" + strID + "'");
                    if (i > 0)
                    {
                        MessageBox.Show("删除成功！");
                        OnLoadModule();
                    }
                }
            }
        }
    }
}
